package org.finesys.chat.core.base.embedding.filter.compairson;

import java.math.BigDecimal;
import java.util.Collection;

public class NumberComparator {

    static int compareAsBigDecimals(Object acutalValue, Object compareValue) {
        return new BigDecimal(acutalValue.toString()).compareTo(new BigDecimal(compareValue.toString()));
    }

    static boolean containsAsBigDecimals(Object actualNumber, Collection<?> comparisonNumbers) {
        BigDecimal actualNumberAsBigDecimal = toBigDecimal(actualNumber);
        return comparisonNumbers.stream().map(NumberComparator::toBigDecimal).anyMatch((comparisonNumberAsBigDecimal) -> {
            return comparisonNumberAsBigDecimal.compareTo(actualNumberAsBigDecimal) == 0;
        });
    }

    private static BigDecimal toBigDecimal(Object actualNumber) {
        if (actualNumber instanceof Integer) {
            return BigDecimal.valueOf((long) actualNumber);
        } else if (actualNumber instanceof Long) {
            return BigDecimal.valueOf((Long) actualNumber);
        } else if (actualNumber instanceof Float) {
            return BigDecimal.valueOf((Float) actualNumber);
        } else if (actualNumber instanceof Double) {
            return BigDecimal.valueOf((Double) actualNumber);
        } else {
            throw new IllegalArgumentException("Unsupported type: " + actualNumber.getClass().getName());
        }
    }


}
